package com.qf.fmall2302.shopcart.service.impl;

import com.qf.fmall2302.shopcart.entity.ShoppingCart;
import com.qf.fmall2302.shopcart.mapper.ShoppingCartMapper;
import com.qf.fmall2302.shopcart.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author dongjl
 * @since 2023-08-24
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ShoppingCart shoppingCart) {

        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = simpleDateFormat.format(date);

        shoppingCart.setCartTime(dateStr);

        this.save(shoppingCart);

    }


    @Override
    public List<ShoppingCart> listbyUserId(Integer userId) {

        ShoppingCartMapper shoppingCartMapper = this.getBaseMapper();
        List<ShoppingCart> data = shoppingCartMapper.listbyUserId(userId);

        return data;
    }

    @Override
    public List<ShoppingCart> listByCids(Integer[] cids) {
        ShoppingCartMapper shoppingCartMapper = this.getBaseMapper();
        List<ShoppingCart> data = shoppingCartMapper.listbyCids(cids);

        return data;
    }
}
